package cn.gxj62;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;

public class addStudent extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        String major = request.getParameter("major");
        boolean sex = request.getParameter("sex").equals("男");
        boolean isLeader = request.getParameter("isLeader").equals("是");

        Student student = new Student(0, name, age, major, sex, isLeader);

        Connection con = mysql.SQLConnect();
        response.setContentType("text/html");
        if (con == null) {
            response.getWriter().println("数据库连接失败<br /><a href=\"javascript:history.back(-1)\">返回上一页</a>");
        } else {
            mysql.addStudent(con, student);
            response.getWriter().println("添加成功<br /><a href=\"javascript:history.back(-1)\">返回上一页</a>");
        }
    }
}
